import html2Canvas from 'html2canvas'
import JsPDF from 'jspdf'
// 可设置页边距，滚动有影响，需要设置滚动置顶
// 能解决图片导出问题
export function getPdfFromHtml(ele,callback) {
  ele = document.getElementById(ele)
  window.pageYoffset = 0; // 滚动置顶
  document.documentElement.scrollTop = 0;
  document.body.scrollTop = 0;
  // ele.style.fontFamily='宋体';
  // ele.style.padding='30px';
  let scale = window.devicePixelRatio * 2
  html2Canvas(ele, {
    // dpi: 300,
    dpi: window.devicePixelRatio * 2, //将分辨率提高到特定的DPI 提高四倍
    scale: 2, //按比例增加分辨率
    logging: false,
    // scale:scale,
    useCORS: true,//允许canvas画布内可以跨域请求外部链接图片, 允许跨域请求。
    allowTaint: false,
    height: ele.offsetHeight,
    width: ele.offsetWidth,
    windowWidth: document.body.scrollWidth,
    windowHeight: document.body.scrollHeight,
    backgroundColor: '#fff'
  }).then(canvas => {
    const _this = this;
    //未生成pdf的html页面高度
    var leftHeight = canvas.height;
    var a4Width = 555.28 // 原A4宽 592 因为要设置边距 20 ,这里要减掉 40
    var a4Height = 801.89 // 原A4高   841 因为要设置边距 20 ,这里要减掉 40
    //一页pdf显示html页面生成的canvas高度;
    var a4HeightRef = Math.floor(canvas.width / a4Width * a4Height);
  
    //pdf页面偏移
    var position = 0;
  
    var pageData = canvas.toDataURL('image/jpeg', 1.0);
  
    var pdf = new JsPDF('x', 'pt', 'a4');
    var index = 1,
        canvas1 = document.createElement('canvas'),
        height;
    pdf.setDisplayMode('fullwidth', 'continuous', 'FullScreen');
  
    function createImpl(canvas) {
      if (leftHeight > 0) {
        index++;
        var checkCount = 0;
        if (leftHeight > a4HeightRef) {
          var i = position + a4HeightRef;
          for (i = position + a4HeightRef; i >= position; i--) {
            var isWrite = true
            for (var j = 0; j < canvas.width; j++) {
              var c = canvas.getContext('2d').getImageData(j, i, 1, 1).data
  
              if (c[0] != 0xff || c[1] != 0xff || c[2] != 0xff) {
                isWrite = false
                break
              }
            }
            if (isWrite) {
              checkCount++
              if (checkCount >= 10) {
                break
              }
            } else {
              checkCount = 0
            }
          }
          height = Math.round(i - position) || Math.min(leftHeight, a4HeightRef);
          if (height <= 0) {
            height = a4HeightRef;
          }
        } else {
          height = leftHeight;
        }
  
        canvas1.width = canvas.width;
        canvas1.height = height;
  
        // console.log(index, 'height:', height, 'pos', position);
  
        var ctx = canvas1.getContext('2d');
        ctx.drawImage(canvas, 0, position, canvas.width, height, 0, 0, canvas.width, height); // 边距这里设置0，不然又黑边
  
        var pageHeight = Math.round(a4Width / canvas.width * height);
        // pdf.setPageSize(null, pageHeight)
        if (position != 0) {
          pdf.addPage();
        }
        // 设置 20px 边距
        pdf.addImage(canvas1.toDataURL('image/jpeg', 1.0), 'JPEG', 20, 20, a4Width, a4Width / canvas1.width * height);
        leftHeight -= height;
        position += height;
        // $('.pdfProgress').text(index + 1);
        // $('.pdfTotal').text(index + Math.ceil(leftHeight / a4HeightRef));
        if (leftHeight > 0) {
          //添加全屏水印
          // const base64 = ''
          // for (let i = 0; i < 6; i++) {
          //   for (let j = 0; j < 5; j++) {
          //     const left = (j * 120) + 20;
          //     // pdf.addImage(base64,'JPEG', left, i*150, 20, 30); // 关掉水印
          //   }
          // }
          setTimeout(createImpl, 500, canvas);
        } else {
          console.log("生成大于页的pdf")

          const base64String = pdf.output('datauristring');
          // console.log(base64String)

          callback(base64String,pdf);

          // pdf.save(pdfFileName + '.pdf');

        }
      }
    }
  
    //当内容未超过pdf一页显示的范围，无需分页
    if (leftHeight < a4HeightRef) {
      pdf.addImage(pageData, 'JPEG', 20, 20, a4Width, a4Width / canvas.width * leftHeight);

      console.log("生成小于一页的pdf")
      const base64String = pdf.output('datauristring');
      callback(base64String,pdf);


      // pdf.save(pdfFileName + '.pdf')
    } else {
      try {
        pdf.deletePage(0);
        setTimeout(createImpl, 500, canvas);
  
      } catch (err) {
        console.log(err);
        callback("");
      }
    }
  })
  
}
  